import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import commonZh from './locales/zh/common.json';
import commonEn from './locales/en/common.json';
import layoutZh from './locales/zh/layout.json';
import layoutEn from './locales/en/layout.json';
import dashboardZh from './locales/zh/dashboard.json';
import dashboardEn from './locales/en/dashboard.json';
import studentZh from './locales/zh/student.json';
import studentEn from './locales/en/student.json';
import classZh from './locales/zh/class.json';
import classEn from './locales/en/class.json';
import gradeZh from './locales/zh/grade.json';
import gradeEn from './locales/en/grade.json';
import teacherZh from './locales/zh/teacher.json';
import teacherEn from './locales/en/teacher.json';
import userZh from './locales/zh/user.json';
import userEn from './locales/en/user.json';
import settingsZh from './locales/zh/settings.json';
import settingsEn from './locales/en/settings.json';
import marketingZh from './locales/zh/marketing.json';
import marketingEn from './locales/en/marketing.json';
import collegeZh from './locales/zh/college.json';
import collegeEn from './locales/en/college.json';
import dormitoryZh from './locales/zh/dormitory.json';
import dormitoryEn from './locales/en/dormitory.json';
import attendanceZh from './locales/zh/attendance.json';
import attendanceEn from './locales/en/attendance.json';
import loginZh from './locales/zh/login.json';
import loginEn from './locales/en/login.json';

const savedLanguage = localStorage.getItem('language') || 'zh';

i18n
  .use(initReactI18next)
  .init({
    fallbackLng: 'zh',
    lng: savedLanguage,
    
    // 命名空间配置
    ns: ['common', 'layout', 'dashboard', 'student', 'class', 'grade', 'teacher', 'user', 'settings', 'marketing', 'college', 'dormitory', 'attendance', 'login'],
    defaultNS: 'common',
    
    interpolation: {
      escapeValue: false
    },
    
    resources: {
      zh: {
        common: commonZh,
        layout: layoutZh,
        dashboard: dashboardZh,
        student: studentZh,
        class: classZh,
        grade: gradeZh,
        teacher: teacherZh,
        user: userZh,
        settings: settingsZh,
        marketing: marketingZh,
        college: collegeZh,
        dormitory: dormitoryZh,
        attendance: attendanceZh,
        login: loginZh
      },
      en: {
        common: commonEn,
        layout: layoutEn,
        dashboard: dashboardEn,
        student: studentEn,
        class: classEn,
        grade: gradeEn,
        teacher: teacherEn,
        user: userEn,
        settings: settingsEn,
        marketing: marketingEn,
        college: collegeEn,
        dormitory: dormitoryEn,
        attendance: attendanceEn,
        login: loginEn
      }
    }
  });

i18n.on('languageChanged', (lng) => {
  localStorage.setItem('language', lng);
});

export default i18n;

